import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCdelect {
    public static void main(String[] args) throws SQLException {
        //1.建立连接
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.用户输入id
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的id：");
        int id = scanner.nextInt();

        //4.拼装sql语句

        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        //5.执行sql
        int ret = statement.executeUpdate();
        System.out.println("ret " + ret);

        //6.回收释放资源
        statement.close();
        connection.close();
    }
}
